{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# \u7b2c\u5341\u56db\u7ae0\uff1a\u6d4b\u8bd5\u3001\u8c03\u8bd5\u548c\u5f02\u5e38\n \u8bd5\u9a8c\u8fd8\u662f\u5f88\u68d2\u7684\uff0c\u4f46\u662f\u8c03\u8bd5\uff1f\u5c31\u6ca1\u90a3\u4e48\u6709\u8da3\u4e86\u3002\u4e8b\u5b9e\u662f\uff0c\u5728Python\u6d4b\u8bd5\u4ee3\u7801\u4e4b\u524d\u6ca1\u6709\u7f16\u8bd1\u5668\u6765\u5206\u6790\u4f60\u7684\u4ee3\u7801\uff0c\u56e0\u6b64\u4f7f\u5f97\u6d4b\u8bd5\u6210\u4e3a\u5f00\u53d1\u7684\u4e00\u4e2a\u91cd\u8981\u90e8\u5206\u3002\u672c\u7ae0\u7684\u76ee\u6807\u662f\u8ba8\u8bba\u4e00\u4e9b\u5173\u4e8e\u6d4b\u8bd5\u3001\u8c03\u8bd5\u548c\u5f02\u5e38\u5904\u7406\u7684\u5e38\u89c1\u95ee\u9898\u3002\u4f46\u662f\u5e76\u4e0d\u662f\u4e3a\u6d4b\u8bd5\u9a71\u52a8\u5f00\u53d1\u6216\u8005\u5355\u5143\u6d4b\u8bd5\u6a21\u5757\u505a\u4e00\u4e2a\u7b80\u8981\u7684\u4ecb\u7ecd\u3002\u56e0\u6b64\uff0c\u7b14\u8005\u5047\u5b9a\u8bfb\u8005\u719f\u6089\u6d4b\u8bd5\u6982\u5ff5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.1 \u6d4b\u8bd5stdout\u8f93\u51fa\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u7a0b\u5e8f\u4e2d\u6709\u4e2a\u65b9\u6cd5\u4f1a\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u4e2d\uff08sys.stdout\uff09\u3002\u4e5f\u5c31\u662f\u8bf4\u5b83\u4f1a\u5c06\u6587\u672c\u6253\u5370\u5230\u5c4f\u5e55\u4e0a\u9762\u3002\n\u4f60\u60f3\u5199\u4e2a\u6d4b\u8bd5\u6765\u8bc1\u660e\u5b83\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u8f93\u5165\uff0c\u76f8\u5e94\u7684\u8f93\u51fa\u80fd\u6b63\u5e38\u663e\u793a\u51fa\u6765\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 unittest.mock \u6a21\u5757\u4e2d\u7684 patch() \u51fd\u6570\uff0c\n\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4e3a\u5355\u4e2a\u6d4b\u8bd5\u6a21\u62df sys.stdout \u7136\u540e\u56de\u6eda\uff0c\n\u5e76\u4e14\u4e0d\u4ea7\u751f\u5927\u91cf\u7684\u4e34\u65f6\u53d8\u91cf\u6216\u5728\u6d4b\u8bd5\u7528\u4f8b\u76f4\u63a5\u66b4\u9732\u72b6\u6001\u53d8\u91cf\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u5728 mymodule \u6a21\u5757\u4e2d\u5b9a\u4e49\u5982\u4e0b\u4e00\u4e2a\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# mymodule.py\n\ndef urlprint(protocol, host, domain):\n    url = '{}://{}.{}'.format(protocol, host, domain)\n    print(url)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5185\u7f6e\u7684 print \u51fd\u6570\u4f1a\u5c06\u8f93\u51fa\u53d1\u9001\u5230 sys.stdout \u3002\n\u4e3a\u4e86\u6d4b\u8bd5\u8f93\u51fa\u771f\u7684\u5728\u90a3\u91cc\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u66ff\u8eab\u5bf9\u8c61\u6765\u6a21\u62df\u5b83\uff0c\u7136\u540e\u4f7f\u7528\u65ad\u8a00\u6765\u786e\u8ba4\u7ed3\u679c\u3002\n\u4f7f\u7528 unittest.mock \u6a21\u5757\u7684 patch() \u65b9\u6cd5\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u5728\u6d4b\u8bd5\u8fd0\u884c\u7684\u4e0a\u4e0b\u6587\u4e2d\u66ff\u6362\u5bf9\u8c61\uff0c\n\u5e76\u4e14\u5f53\u6d4b\u8bd5\u5b8c\u6210\u65f6\u5019\u81ea\u52a8\u8fd4\u56de\u5b83\u4eec\u7684\u539f\u6709\u72b6\u6001\u3002\u4e0b\u9762\u662f\u5bf9 mymodule \u6a21\u5757\u7684\u6d4b\u8bd5\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from io import StringIO\nfrom unittest import TestCase\nfrom unittest.mock import patch\nimport mymodule\n\nclass TestURLPrint(TestCase):\n    def test_url_gets_to_stdout(self):\n        protocol = 'http'\n        host = 'www'\n        domain = 'example.com'\n        expected_url = '{}://{}.{}\\n'.format(protocol, host, domain)\n\n        with patch('sys.stdout', new=StringIO()) as fake_out:\n            mymodule.urlprint(protocol, host, domain)\n            self.assertEqual(fake_out.getvalue(), expected_url)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "urlprint() \u51fd\u6570\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff0c\u6d4b\u8bd5\u65b9\u6cd5\u5f00\u59cb\u4f1a\u5148\u8bbe\u7f6e\u6bcf\u4e00\u4e2a\u53c2\u6570\u7684\u503c\u3002\nexpected_url \u53d8\u91cf\u88ab\u8bbe\u7f6e\u6210\u5305\u542b\u671f\u671b\u7684\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "unittest.mock.patch() \u51fd\u6570\u88ab\u7528\u4f5c\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u4f7f\u7528 StringIO \u5bf9\u8c61\u6765\u4ee3\u66ff sys.stdout .\nfake_out \u53d8\u91cf\u662f\u5728\u8be5\u8fdb\u7a0b\u4e2d\u88ab\u521b\u5efa\u7684\u6a21\u62df\u5bf9\u8c61\u3002\n\u5728with\u8bed\u53e5\u4e2d\u4f7f\u7528\u5b83\u53ef\u4ee5\u6267\u884c\u5404\u79cd\u68c0\u67e5\u3002\u5f53with\u8bed\u53e5\u7ed3\u675f\u65f6\uff0cpatch \u4f1a\u5c06\u6240\u6709\u4e1c\u897f\u6062\u590d\u5230\u6d4b\u8bd5\u5f00\u59cb\u524d\u7684\u72b6\u6001\u3002\n\u6709\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u7684\u662f\u67d0\u4e9b\u5bf9Python\u7684C\u6269\u5c55\u53ef\u80fd\u4f1a\u5ffd\u7565\u6389 sys.stdout \u7684\u914d\u7f6e\u800c\u76f4\u63a5\u5199\u5165\u5230\u6807\u51c6\u8f93\u51fa\u4e2d\u3002\n\u9650\u4e8e\u7bc7\u5e45\uff0c\u672c\u8282\u4e0d\u4f1a\u6d89\u53ca\u5230\u8fd9\u65b9\u9762\u7684\u8bb2\u89e3\uff0c\u5b83\u9002\u7528\u4e8e\u7eafPython\u4ee3\u7801\u3002\n\u5982\u679c\u4f60\u771f\u7684\u9700\u8981\u5728C\u6269\u5c55\u4e2d\u6355\u83b7I/O\uff0c\u4f60\u53ef\u4ee5\u5148\u6253\u5f00\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u8be5\u6587\u4ef6\u4e2d\u3002\n\u66f4\u591a\u5173\u4e8e\u6355\u83b7\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u6355\u83b7I/O\u548c StringIO \u5bf9\u8c61\u8bf7\u53c2\u96055.6\u5c0f\u8282\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.2 \u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u7ed9\u5bf9\u8c61\u6253\u8865\u4e01\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5199\u7684\u5355\u5143\u6d4b\u8bd5\u4e2d\u9700\u8981\u7ed9\u6307\u5b9a\u7684\u5bf9\u8c61\u6253\u8865\u4e01\uff0c\n\u7528\u6765\u65ad\u8a00\u5b83\u4eec\u5728\u6d4b\u8bd5\u4e2d\u7684\u671f\u671b\u884c\u4e3a\uff08\u6bd4\u5982\uff0c\u65ad\u8a00\u88ab\u8c03\u7528\u65f6\u7684\u53c2\u6570\u4e2a\u6570\uff0c\u8bbf\u95ee\u6307\u5b9a\u7684\u5c5e\u6027\u7b49\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "unittest.mock.patch() \u51fd\u6570\u53ef\u88ab\u7528\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\npatch() \u8fd8\u53ef\u88ab\u7528\u4f5c\u4e00\u4e2a\u88c5\u9970\u5668\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6216\u5355\u72ec\u4f7f\u7528\uff0c\u5c3d\u7ba1\u5e76\u4e0d\u5e38\u89c1\u3002\n\u4f8b\u5982\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06\u5b83\u5f53\u505a\u88c5\u9970\u5668\u4f7f\u7528\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from unittest.mock import patch\nimport example\n\n@patch('example.func')\ndef test1(x, mock_func):\n    example.func(x)       # Uses patched example.func\n    mock_func.assert_called_with(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b83\u8fd8\u53ef\u4ee5\u88ab\u5f53\u505a\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with patch('example.func') as mock_func:\n    example.func(x)      # Uses patched example.func\n    mock_func.assert_called_with(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u4f60\u8fd8\u53ef\u4ee5\u624b\u52a8\u7684\u4f7f\u7528\u5b83\u6253\u8865\u4e01\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p = patch('example.func')\nmock_func = p.start()\nexample.func(x)\nmock_func.assert_called_with(x)\np.stop()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u53ef\u80fd\u7684\u8bdd\uff0c\u4f60\u80fd\u591f\u53e0\u52a0\u88c5\u9970\u5668\u548c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u7ed9\u591a\u4e2a\u5bf9\u8c61\u6253\u8865\u4e01\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@patch('example.func1')\n@patch('example.func2')\n@patch('example.func3')\ndef test1(mock1, mock2, mock3):\n    ...\n\ndef test2():\n    with patch('example.patch1') as mock1, \\\n         patch('example.patch2') as mock2, \\\n         patch('example.patch3') as mock3:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "patch() \u63a5\u53d7\u4e00\u4e2a\u5df2\u5b58\u5728\u5bf9\u8c61\u7684\u5168\u8def\u5f84\u540d\uff0c\u5c06\u5176\u66ff\u6362\u4e3a\u4e00\u4e2a\u65b0\u7684\u503c\u3002\n\u539f\u6765\u7684\u503c\u4f1a\u5728\u88c5\u9970\u5668\u51fd\u6570\u6216\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5b8c\u6210\u540e\u81ea\u52a8\u6062\u590d\u56de\u6765\u3002\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u503c\u4f1a\u88ab MagicMock \u5b9e\u4f8b\u66ff\u4ee3\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = 42\nwith patch('__main__.x'):\n    print(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8fc7\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u7ed9 patch() \u63d0\u4f9b\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6765\u5c06\u503c\u66ff\u6362\u6210\u4efb\u4f55\u4f60\u60f3\u8981\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with patch('__main__.x', 'patched_value'):\n    print(x)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u88ab\u7528\u6765\u4f5c\u4e3a\u66ff\u6362\u503c\u7684 MagicMock \u5b9e\u4f8b\u80fd\u591f\u6a21\u62df\u53ef\u8c03\u7528\u5bf9\u8c61\u548c\u5b9e\u4f8b\u3002\n\u4ed6\u4eec\u8bb0\u5f55\u5bf9\u8c61\u7684\u4f7f\u7528\u4fe1\u606f\u5e76\u5141\u8bb8\u4f60\u6267\u884c\u65ad\u8a00\u68c0\u67e5\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from unittest.mock import MagicMock\nm = MagicMock(return_value = 10)\nm(1, 2, debug=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.assert_called_with(1, 2, debug=True)\nm.assert_called_with(1, 2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.upper.return_value = 'HELLO'\nm.upper('hello')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "assert m.upper.called"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.split.return_value = ['hello', 'world']\nm.split('hello world')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.split.assert_called_with('hello world')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m['blah']"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.__getitem__.called"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.__getitem__.assert_called_with('blah')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u822c\u6765\u8bb2\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u4f1a\u5728\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u4e2d\u5b8c\u6210\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u5df2\u7ecf\u6709\u4e86\u50cf\u4e0b\u9762\u8fd9\u6837\u7684\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# example.py\nfrom urllib.request import urlopen\nimport csv\n\ndef dowprices():\n    u = urlopen('http://finance.yahoo.com/d/quotes.csv?s=@^DJI&f=sl1')\n    lines = (line.decode('utf-8') for line in u)\n    rows = (row for row in csv.reader(lines) if len(row) == 2)\n    prices = { name:float(price) for name, price in rows }\n    return prices"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5e38\u6765\u8bb2\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4f7f\u7528 urlopen() \u4eceWeb\u4e0a\u9762\u83b7\u53d6\u6570\u636e\u5e76\u89e3\u6790\u5b83\u3002\n\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u7ed9\u5b83\u4e00\u4e2a\u9884\u5148\u5b9a\u4e49\u597d\u7684\u6570\u636e\u96c6\u3002\u4e0b\u9762\u662f\u4f7f\u7528\u8865\u4e01\u64cd\u4f5c\u7684\u4f8b\u5b50:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import unittest\nfrom unittest.mock import patch\nimport io\nimport example\n\nsample_data = io.BytesIO(b'''\\\n\"IBM\",91.1\\r\n\"AA\",13.25\\r\n\"MSFT\",27.72\\r\n\\r\n''')\n\nclass Tests(unittest.TestCase):\n    @patch('example.urlopen', return_value=sample_data)\n    def test_dowprices(self, mock_urlopen):\n        p = example.dowprices()\n        self.assertTrue(mock_urlopen.called)\n        self.assertEqual(p,\n                         {'IBM': 91.1,\n                          'AA': 13.25,\n                          'MSFT' : 27.72})\n\nif __name__ == '__main__':\n    unittest.main()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u4f8b\u4e2d\uff0c\u4f4d\u4e8e example \u6a21\u5757\u4e2d\u7684 urlopen() \u51fd\u6570\u88ab\u4e00\u4e2a\u6a21\u62df\u5bf9\u8c61\u66ff\u4ee3\uff0c\n\u8be5\u5bf9\u8c61\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6d4b\u8bd5\u6570\u636e\u7684 ByteIO()."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u70b9\uff0c\u5728\u6253\u8865\u4e01\u65f6\u6211\u4eec\u4f7f\u7528\u4e86 example.urlopen \u6765\u4ee3\u66ff urllib.request.urlopen \u3002\n\u5f53\u4f60\u521b\u5efa\u8865\u4e01\u7684\u65f6\u5019\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528\u5b83\u4eec\u5728\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u7684\u540d\u79f0\u3002\n\u7531\u4e8e\u6d4b\u8bd5\u4ee3\u7801\u4f7f\u7528\u4e86 from urllib.request import urlopen ,\u90a3\u4e48 dowprices() \u51fd\u6570\n\u4e2d\u4f7f\u7528\u7684 urlopen() \u51fd\u6570\u5b9e\u9645\u4e0a\u5c31\u4f4d\u4e8e example \u6a21\u5757\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u5b9e\u9645\u4e0a\u53ea\u662f\u5bf9 unittest.mock \u6a21\u5757\u7684\u4e00\u6b21\u6d45\u5c1d\u8f84\u6b62\u3002\n\u66f4\u591a\u66f4\u9ad8\u7ea7\u7684\u7279\u6027\uff0c\u8bf7\u53c2\u8003 \u5b98\u65b9\u6587\u6863"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.3 \u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u6d4b\u8bd5\u5f02\u5e38\u60c5\u51b5\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5199\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u6765\u51c6\u786e\u7684\u5224\u65ad\u67d0\u4e2a\u5f02\u5e38\u662f\u5426\u88ab\u629b\u51fa\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5f02\u5e38\u7684\u6d4b\u8bd5\u53ef\u4f7f\u7528 assertRaises() \u65b9\u6cd5\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u6d4b\u8bd5\u67d0\u4e2a\u51fd\u6570\u629b\u51fa\u4e86 ValueError \u5f02\u5e38\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import unittest\n\n# A simple function to illustrate\ndef parse_int(s):\n    return int(s)\n\nclass TestConversion(unittest.TestCase):\n    def test_bad_int(self):\n        self.assertRaises(ValueError, parse_int, 'N/A')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u6d4b\u8bd5\u5f02\u5e38\u7684\u5177\u4f53\u503c\uff0c\u9700\u8981\u7528\u5230\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import errno\n\nclass TestIO(unittest.TestCase):\n    def test_file_not_found(self):\n        try:\n            f = open('/file/not/found')\n        except IOError as e:\n            self.assertEqual(e.errno, errno.ENOENT)\n\n        else:\n            self.fail('IOError not raised')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "assertRaises() \u65b9\u6cd5\u4e3a\u6d4b\u8bd5\u5f02\u5e38\u5b58\u5728\u6027\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u4fbf\u65b9\u6cd5\u3002\n\u4e00\u4e2a\u5e38\u89c1\u7684\u9677\u9631\u662f\u624b\u52a8\u53bb\u8fdb\u884c\u5f02\u5e38\u68c0\u6d4b\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class TestConversion(unittest.TestCase):\n    def test_bad_int(self):\n        try:\n            r = parse_int('N/A')\n        except ValueError as e:\n            self.assertEqual(type(e), ValueError)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u65b9\u6cd5\u7684\u95ee\u9898\u5728\u4e8e\u5b83\u5f88\u5bb9\u6613\u9057\u6f0f\u5176\u4ed6\u60c5\u51b5\uff0c\u6bd4\u5982\u6ca1\u6709\u4efb\u4f55\u5f02\u5e38\u629b\u51fa\u7684\u65f6\u5019\u3002\n\u90a3\u4e48\u4f60\u8fd8\u5f97\u9700\u8981\u589e\u52a0\u53e6\u5916\u7684\u68c0\u6d4b\u8fc7\u7a0b\uff0c\u5982\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class TestConversion(unittest.TestCase):\n    def test_bad_int(self):\n        try:\n            r = parse_int('N/A')\n        except ValueError as e:\n            self.assertEqual(type(e), ValueError)\n        else:\n            self.fail('ValueError not raised')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "assertRaises() \u65b9\u6cd5\u4f1a\u5904\u7406\u6240\u6709\u7ec6\u8282\uff0c\u56e0\u6b64\u4f60\u5e94\u8be5\u4f7f\u7528\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "assertRaises() \u7684\u4e00\u4e2a\u7f3a\u70b9\u662f\u5b83\u6d4b\u4e0d\u4e86\u5f02\u5e38\u5177\u4f53\u7684\u503c\u662f\u591a\u5c11\u3002\n\u4e3a\u4e86\u6d4b\u8bd5\u5f02\u5e38\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 assertRaisesRegex() \u65b9\u6cd5\uff0c\n\u5b83\u53ef\u540c\u65f6\u6d4b\u8bd5\u5f02\u5e38\u7684\u5b58\u5728\u4ee5\u53ca\u901a\u8fc7\u6b63\u5219\u5f0f\u5339\u914d\u5f02\u5e38\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class TestConversion(unittest.TestCase):\n    def test_bad_int(self):\n        self.assertRaisesRegex(ValueError, 'invalid literal .*',\n                                       parse_int, 'N/A')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "assertRaises() \u548c assertRaisesRegex()\n\u8fd8\u6709\u4e00\u4e2a\u5bb9\u6613\u5ffd\u7565\u7684\u5730\u65b9\u5c31\u662f\u5b83\u4eec\u8fd8\u80fd\u88ab\u5f53\u505a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4f7f\u7528\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class TestConversion(unittest.TestCase):\n    def test_bad_int(self):\n        with self.assertRaisesRegex(ValueError, 'invalid literal .*'):\n            r = parse_int('N/A')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u4f60\u7684\u6d4b\u8bd5\u6d89\u53ca\u5230\u591a\u4e2a\u6267\u884c\u6b65\u9aa4\u7684\u65f6\u5019\u8fd9\u79cd\u65b9\u6cd5\u5c31\u5f88\u6709\u7528\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.4 \u5c06\u6d4b\u8bd5\u8f93\u51fa\u7528\u65e5\u5fd7\u8bb0\u5f55\u5230\u6587\u4ef6\u4e2d\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5e0c\u671b\u5c06\u5355\u5143\u6d4b\u8bd5\u7684\u8f93\u51fa\u5199\u5230\u5230\u67d0\u4e2a\u6587\u4ef6\u4e2d\u53bb\uff0c\u800c\u4e0d\u662f\u6253\u5370\u5230\u6807\u51c6\u8f93\u51fa\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\u4e00\u4e2a\u5e38\u89c1\u6280\u672f\u5c31\u662f\u5728\u6d4b\u8bd5\u6587\u4ef6\u5e95\u90e8\u52a0\u5165\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u7247\u6bb5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import unittest\n\nclass MyTest(unittest.TestCase):\n    pass\n\nif __name__ == '__main__':\n    unittest.main()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u7684\u8bdd\u6d4b\u8bd5\u6587\u4ef6\u5c31\u662f\u53ef\u6267\u884c\u7684\uff0c\u5e76\u4e14\u4f1a\u5c06\u8fd0\u884c\u6d4b\u8bd5\u7684\u7ed3\u679c\u6253\u5370\u5230\u6807\u51c6\u8f93\u51fa\u4e0a\u3002\n\u5982\u679c\u4f60\u60f3\u91cd\u5b9a\u5411\u8f93\u51fa\uff0c\u5c31\u9700\u8981\u50cf\u4e0b\u9762\u8fd9\u6837\u4fee\u6539 main() \u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import sys\n\ndef main(out=sys.stderr, verbosity=2):\n    loader = unittest.TestLoader()\n    suite = loader.loadTestsFromModule(sys.modules[__name__])\n    unittest.TextTestRunner(out,verbosity=verbosity).run(suite)\n\nif __name__ == '__main__':\n    with open('testing.out', 'w') as f:\n        main(f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u672c\u8282\u611f\u5174\u8da3\u7684\u90e8\u5206\u5e76\u4e0d\u662f\u5c06\u6d4b\u8bd5\u7ed3\u679c\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\n\u800c\u662f\u901a\u8fc7\u8fd9\u6837\u505a\u5411\u4f60\u5c55\u793a\u4e86 unittest \u6a21\u5757\u4e2d\u4e00\u4e9b\u503c\u5f97\u5173\u6ce8\u7684\u5185\u90e8\u5de5\u4f5c\u539f\u7406\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "unittest \u6a21\u5757\u9996\u5148\u4f1a\u7ec4\u88c5\u4e00\u4e2a\u6d4b\u8bd5\u5957\u4ef6\u3002\n\u8fd9\u4e2a\u6d4b\u8bd5\u5957\u4ef6\u5305\u542b\u4e86\u4f60\u5b9a\u4e49\u7684\u5404\u79cd\u65b9\u6cd5\u3002\u4e00\u65e6\u5957\u4ef6\u7ec4\u88c5\u5b8c\u6210\uff0c\u5b83\u6240\u5305\u542b\u7684\u6d4b\u8bd5\u5c31\u53ef\u4ee5\u88ab\u6267\u884c\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e24\u6b65\u662f\u5206\u5f00\u7684\uff0cunittest.TestLoader \u5b9e\u4f8b\u88ab\u7528\u6765\u7ec4\u88c5\u6d4b\u8bd5\u5957\u4ef6\u3002\nloadTestsFromModule() \u662f\u5b83\u5b9a\u4e49\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u6765\u6536\u96c6\u6d4b\u8bd5\u7528\u4f8b\u3002\n\u5b83\u4f1a\u4e3a TestCase \u7c7b\u626b\u63cf\u67d0\u4e2a\u6a21\u5757\u5e76\u5c06\u5176\u4e2d\u7684\u6d4b\u8bd5\u65b9\u6cd5\u63d0\u53d6\u51fa\u6765\u3002\n\u5982\u679c\u4f60\u60f3\u8fdb\u884c\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\n\u53ef\u4ee5\u4f7f\u7528 loadTestsFromTestCase() \u65b9\u6cd5\u6765\u4ece\u67d0\u4e2a\u7ee7\u627fTestCase\u7684\u7c7b\u4e2d\u63d0\u53d6\u6d4b\u8bd5\u65b9\u6cd5\u3002\nTextTestRunner \u7c7b\u662f\u4e00\u4e2a\u6d4b\u8bd5\u8fd0\u884c\u7c7b\u7684\u4f8b\u5b50\uff0c\n\u8fd9\u4e2a\u7c7b\u7684\u4e3b\u8981\u7528\u9014\u662f\u6267\u884c\u67d0\u4e2a\u6d4b\u8bd5\u5957\u4ef6\u4e2d\u5305\u542b\u7684\u6d4b\u8bd5\u65b9\u6cd5\u3002\n\u8fd9\u4e2a\u7c7b\u8ddf\u6267\u884c unittest.main() \u51fd\u6570\u6240\u4f7f\u7528\u7684\u6d4b\u8bd5\u8fd0\u884c\u5668\u662f\u4e00\u6837\u7684\u3002\n\u4e0d\u8fc7\uff0c\u6211\u4eec\u5728\u8fd9\u91cc\u5bf9\u5b83\u8fdb\u884c\u4e86\u4e00\u4e9b\u5217\u5e95\u5c42\u914d\u7f6e\uff0c\u5305\u62ec\u8f93\u51fa\u6587\u4ef6\u548c\u63d0\u5347\u7ea7\u522b\u3002\n\u5c3d\u7ba1\u672c\u8282\u4f8b\u5b50\u4ee3\u7801\u5f88\u5c11\uff0c\u4f46\u662f\u80fd\u6307\u5bfc\u4f60\u5982\u4f55\u5bf9 unittest \u6846\u67b6\u8fdb\u884c\u66f4\u8fdb\u4e00\u6b65\u7684\u81ea\u5b9a\u4e49\u3002\n\u8981\u60f3\u81ea\u5b9a\u4e49\u6d4b\u8bd5\u5957\u4ef6\u7684\u88c5\u914d\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5bf9 TestLoader \u7c7b\u6267\u884c\u66f4\u591a\u7684\u64cd\u4f5c\u3002\n\u4e3a\u4e86\u81ea\u5b9a\u4e49\u6d4b\u8bd5\u8fd0\u884c\uff0c\u4f60\u53ef\u4ee5\u6784\u9020\u4e00\u4e2a\u81ea\u5df1\u7684\u6d4b\u8bd5\u8fd0\u884c\u7c7b\u6765\u6a21\u62df TextTestRunner \u7684\u529f\u80fd\u3002\n\u800c\u8fd9\u4e9b\u5df2\u7ecf\u8d85\u51fa\u4e86\u672c\u8282\u7684\u8303\u56f4\u3002unittest \u6a21\u5757\u7684\u6587\u6863\u5bf9\u5e95\u5c42\u5b9e\u73b0\u539f\u7406\u6709\u66f4\u6df1\u5165\u7684\u8bb2\u89e3\uff0c\u53ef\u4ee5\u53bb\u770b\u770b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.5 \u5ffd\u7565\u6216\u671f\u671b\u6d4b\u8bd5\u5931\u8d25\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u5ffd\u7565\u6216\u6807\u8bb0\u67d0\u4e9b\u6d4b\u8bd5\u4f1a\u6309\u7167\u9884\u671f\u8fd0\u884c\u5931\u8d25\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "unittest \u6a21\u5757\u6709\u88c5\u9970\u5668\u53ef\u7528\u6765\u63a7\u5236\u5bf9\u6307\u5b9a\u6d4b\u8bd5\u65b9\u6cd5\u7684\u5904\u7406\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import unittest\nimport os\nimport platform\n\nclass Tests(unittest.TestCase):\n    def test_0(self):\n        self.assertTrue(True)\n\n    @unittest.skip('skipped test')\n    def test_1(self):\n        self.fail('should have failed!')\n\n    @unittest.skipIf(os.name=='posix', 'Not supported on Unix')\n    def test_2(self):\n        import winreg\n\n    @unittest.skipUnless(platform.system() == 'Darwin', 'Mac specific test')\n    def test_3(self):\n        self.assertTrue(True)\n\n    @unittest.expectedFailure\n    def test_4(self):\n        self.assertEqual(2+2, 5)\n\nif __name__ == '__main__':\n    unittest.main()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u5728Mac\u4e0a\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u4f60\u4f1a\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 testsample.py -v\ntest_0 (__main__.Tests) ... ok\ntest_1 (__main__.Tests) ... skipped 'skipped test'\ntest_2 (__main__.Tests) ... skipped 'Not supported on Unix'\ntest_3 (__main__.Tests) ... ok\ntest_4 (__main__.Tests) ... expected failure\n\n----------------------------------------------------------------------\nRan 5 tests in 0.002s\n\nOK (skipped=2, expected failures=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "skip() \u88c5\u9970\u5668\u80fd\u88ab\u7528\u6765\u5ffd\u7565\u67d0\u4e2a\u4f60\u4e0d\u60f3\u8fd0\u884c\u7684\u6d4b\u8bd5\u3002\nskipIf() \u548c skipUnless()\n\u5bf9\u4e8e\u4f60\u53ea\u60f3\u5728\u67d0\u4e2a\u7279\u5b9a\u5e73\u53f0\u6216Python\u7248\u672c\u6216\u5176\u4ed6\u4f9d\u8d56\u6210\u7acb\u65f6\u624d\u8fd0\u884c\u6d4b\u8bd5\u7684\u65f6\u5019\u975e\u5e38\u6709\u7528\u3002\n\u4f7f\u7528 @expected \u7684\u5931\u8d25\u88c5\u9970\u5668\u6765\u6807\u8bb0\u90a3\u4e9b\u786e\u5b9a\u4f1a\u5931\u8d25\u7684\u6d4b\u8bd5\uff0c\u5e76\u4e14\u5bf9\u8fd9\u4e9b\u6d4b\u8bd5\u4f60\u4e0d\u60f3\u8ba9\u6d4b\u8bd5\u6846\u67b6\u6253\u5370\u66f4\u591a\u4fe1\u606f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5ffd\u7565\u65b9\u6cd5\u7684\u88c5\u9970\u5668\u8fd8\u53ef\u4ee5\u88ab\u7528\u6765\u88c5\u9970\u6574\u4e2a\u6d4b\u8bd5\u7c7b\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@unittest.skipUnless(platform.system() == 'Darwin', 'Mac specific tests')\nclass DarwinTests(unittest.TestCase):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.6 \u5904\u7406\u591a\u4e2a\u5f02\u5e38\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u4ee3\u7801\u7247\u6bb5\u53ef\u80fd\u4f1a\u629b\u51fa\u591a\u4e2a\u4e0d\u540c\u7684\u5f02\u5e38\uff0c\u600e\u6837\u624d\u80fd\u4e0d\u521b\u5efa\u5927\u91cf\u91cd\u590d\u4ee3\u7801\u5c31\u80fd\u5904\u7406\u6240\u6709\u7684\u53ef\u80fd\u5f02\u5e38\u5462\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u53ef\u4ee5\u7528\u5355\u4e2a\u4ee3\u7801\u5757\u5904\u7406\u4e0d\u540c\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u653e\u5165\u4e00\u4e2a\u5143\u7ec4\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    client_obj.get_url(url)\nexcept (URLError, ValueError, SocketTimeout):\n    client_obj.remove_url(url)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5143\u7956\u4e2d\u4efb\u4f55\u4e00\u4e2a\u5f02\u5e38\u53d1\u751f\u65f6\u90fd\u4f1a\u6267\u884c remove_url() \u65b9\u6cd5\u3002\n\u5982\u679c\u4f60\u60f3\u5bf9\u5176\u4e2d\u67d0\u4e2a\u5f02\u5e38\u8fdb\u884c\u4e0d\u540c\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u5c06\u5176\u653e\u5165\u53e6\u5916\u4e00\u4e2a except \u8bed\u53e5\u4e2d\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    client_obj.get_url(url)\nexcept (URLError, ValueError):\n    client_obj.remove_url(url)\nexcept SocketTimeout:\n    client_obj.handle_url_timeout(url)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u591a\u7684\u5f02\u5e38\u4f1a\u6709\u5c42\u7ea7\u5173\u7cfb\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f60\u53ef\u80fd\u4f7f\u7528\u5b83\u4eec\u7684\u4e00\u4e2a\u57fa\u7c7b\u6765\u6355\u83b7\u6240\u6709\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    f = open(filename)\nexcept (FileNotFoundError, PermissionError):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u88ab\u91cd\u5199\u4e3a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    f = open(filename)\nexcept OSError:\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "OSError \u662f FileNotFoundError \u548c PermissionError \u5f02\u5e38\u7684\u57fa\u7c7b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u5904\u7406\u591a\u4e2a\u5f02\u5e38\u672c\u8eab\u5e76\u6ca1\u4ec0\u4e48\u7279\u6b8a\u7684\uff0c\u4e0d\u8fc7\u4f60\u53ef\u4ee5\u4f7f\u7528 as \u5173\u952e\u5b57\u6765\u83b7\u5f97\u88ab\u629b\u51fa\u5f02\u5e38\u7684\u5f15\u7528\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    f = open(filename)\nexcept OSError as e:\n    if e.errno == errno.ENOENT:\n        logger.error('File not found')\n    elif e.errno == errno.EACCES:\n        logger.error('Permission denied')\n    else:\n        logger.error('Unexpected error: %d', e.errno)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c e \u53d8\u91cf\u6307\u5411\u4e00\u4e2a\u88ab\u629b\u51fa\u7684 OSError \u5f02\u5e38\u5b9e\u4f8b\u3002\n\u8fd9\u4e2a\u5728\u4f60\u60f3\u66f4\u8fdb\u4e00\u6b65\u5206\u6790\u8fd9\u4e2a\u5f02\u5e38\u7684\u65f6\u5019\u4f1a\u5f88\u6709\u7528\uff0c\u6bd4\u5982\u57fa\u4e8e\u67d0\u4e2a\u72b6\u6001\u7801\u6765\u5904\u7406\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u540c\u65f6\u8fd8\u8981\u6ce8\u610f\u7684\u65f6\u5019 except \u8bed\u53e5\u662f\u987a\u5e8f\u68c0\u67e5\u7684\uff0c\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u4f1a\u6267\u884c\u3002\n\u4f60\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u6784\u9020\u591a\u4e2a except \u540c\u65f6\u5339\u914d\u7684\u60c5\u5f62\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('missing')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    f = open('missing')\nexcept OSError:\n    print('It failed')\nexcept FileNotFoundError:\n    print('File not found')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u7684 FileNotFoundError \u8bed\u53e5\u5e76\u6ca1\u6709\u6267\u884c\u7684\u539f\u56e0\u662f OSError \u66f4\u4e00\u822c\uff0c\u5b83\u53ef\u5339\u914d FileNotFoundError \u5f02\u5e38\uff0c\n\u4e8e\u662f\u5c31\u662f\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u3002\n\u5728\u8c03\u8bd5\u7684\u65f6\u5019\uff0c\u5982\u679c\u4f60\u5bf9\u67d0\u4e2a\u7279\u5b9a\u5f02\u5e38\u7684\u7c7b\u6210\u5c42\u7ea7\u5173\u7cfb\u4e0d\u662f\u5f88\u786e\u5b9a\uff0c\n\u4f60\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u8be5\u5f02\u5e38\u7684 __mro__ \u5c5e\u6027\u6765\u5feb\u901f\u6d4f\u89c8\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "FileNotFoundError.__mro__"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u5217\u8868\u4e2d\u4efb\u4f55\u4e00\u4e2a\u76f4\u5230 BaseException \u7684\u7c7b\u90fd\u80fd\u88ab\u7528\u4e8e except \u8bed\u53e5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.7 \u6355\u83b7\u6240\u6709\u5f02\u5e38\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u600e\u6837\u6355\u83b7\u4ee3\u7801\u4e2d\u7684\u6240\u6709\u5f02\u5e38\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u60f3\u8981\u6355\u83b7\u6240\u6709\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u76f4\u63a5\u6355\u83b7 Exception \u5373\u53ef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n   ...\nexcept Exception as e:\n   ...\n   log('Reason:', e)       # Important!"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u5c06\u4f1a\u6355\u83b7\u9664\u4e86 SystemExit \u3001 KeyboardInterrupt \u548c GeneratorExit \u4e4b\u5916\u7684\u6240\u6709\u5f02\u5e38\u3002\n\u5982\u679c\u4f60\u8fd8\u60f3\u6355\u83b7\u8fd9\u4e09\u4e2a\u5f02\u5e38\uff0c\u5c06 Exception \u6539\u6210 BaseException \u5373\u53ef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6355\u83b7\u6240\u6709\u5f02\u5e38\u901a\u5e38\u662f\u7531\u4e8e\u7a0b\u5e8f\u5458\u5728\u67d0\u4e9b\u590d\u6742\u64cd\u4f5c\u4e2d\u5e76\u4e0d\u80fd\u8bb0\u4f4f\u6240\u6709\u53ef\u80fd\u7684\u5f02\u5e38\u3002\n\u5982\u679c\u4f60\u4e0d\u662f\u5f88\u7ec6\u5fc3\u7684\u4eba\uff0c\u8fd9\u4e5f\u662f\u7f16\u5199\u4e0d\u6613\u8c03\u8bd5\u4ee3\u7801\u7684\u4e00\u4e2a\u7b80\u5355\u65b9\u6cd5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u56e0\u5982\u6b64\uff0c\u5982\u679c\u4f60\u9009\u62e9\u6355\u83b7\u6240\u6709\u5f02\u5e38\uff0c\u90a3\u4e48\u5728\u67d0\u4e2a\u5730\u65b9\uff08\u6bd4\u5982\u65e5\u5fd7\u6587\u4ef6\u3001\u6253\u5370\u5f02\u5e38\u5230\u5c4f\u5e55\uff09\u6253\u5370\u786e\u5207\u539f\u56e0\u5c31\u6bd4\u8f83\u91cd\u8981\u4e86\u3002\n\u5982\u679c\u4f60\u6ca1\u6709\u8fd9\u6837\u505a\uff0c\u6709\u65f6\u5019\u4f60\u770b\u5230\u5f02\u5e38\u6253\u5370\u65f6\u53ef\u80fd\u6478\u4e0d\u7740\u5934\u8111\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def parse_int(s):\n    try:\n        n = int(v)\n    except Exception:\n        print(\"Couldn't parse\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8bd5\u7740\u8fd0\u884c\u8fd9\u4e2a\u51fd\u6570\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "parse_int('n/a')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "parse_int('42')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u65f6\u5019\u4f60\u5c31\u4f1a\u6320\u5934\u60f3\uff1a\u201c\u8fd9\u548b\u56de\u4e8b\u554a\uff1f\u201d \u5047\u5982\u4f60\u50cf\u4e0b\u9762\u8fd9\u6837\u91cd\u5199\u8fd9\u4e2a\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def parse_int(s):\n    try:\n        n = int(v)\n    except Exception as e:\n        print(\"Couldn't parse\")\n        print('Reason:', e)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u65f6\u5019\u4f60\u80fd\u83b7\u53d6\u5982\u4e0b\u8f93\u51fa\uff0c\u6307\u660e\u4e86\u6709\u4e2a\u7f16\u7a0b\u9519\u8bef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "parse_int('42')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u660e\u663e\uff0c\u4f60\u5e94\u8be5\u5c3d\u53ef\u80fd\u5c06\u5f02\u5e38\u5904\u7406\u5668\u5b9a\u4e49\u7684\u7cbe\u51c6\u4e00\u4e9b\u3002\n\u4e0d\u8fc7\uff0c\u8981\u662f\u4f60\u5fc5\u987b\u6355\u83b7\u6240\u6709\u5f02\u5e38\uff0c\u786e\u4fdd\u6253\u5370\u6b63\u786e\u7684\u8bca\u65ad\u4fe1\u606f\u6216\u5c06\u5f02\u5e38\u4f20\u64ad\u51fa\u53bb\uff0c\u8fd9\u6837\u4e0d\u4f1a\u4e22\u5931\u6389\u5f02\u5e38\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.8 \u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f60\u6784\u5efa\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u60f3\u5c06\u5e95\u5c42\u5f02\u5e38\u5305\u88c5\u6210\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521b\u5efa\u65b0\u7684\u5f02\u5e38\u5f88\u7b80\u5355\u2014\u2014\u5b9a\u4e49\u65b0\u7684\u7c7b\uff0c\u8ba9\u5b83\u7ee7\u627f\u81ea Exception \uff08\u6216\u8005\u662f\u4efb\u4f55\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5f02\u5e38\u7c7b\u578b\uff09\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u7f16\u5199\u7f51\u7edc\u76f8\u5173\u7684\u7a0b\u5e8f\uff0c\u4f60\u53ef\u80fd\u4f1a\u5b9a\u4e49\u4e00\u4e9b\u7c7b\u4f3c\u5982\u4e0b\u7684\u5f02\u5e38\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class NetworkError(Exception):\n    pass\n\nclass HostnameError(NetworkError):\n    pass\n\nclass TimeoutError(NetworkError):\n    pass\n\nclass ProtocolError(NetworkError):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u540e\u7528\u6237\u5c31\u53ef\u4ee5\u50cf\u901a\u5e38\u90a3\u6837\u4f7f\u7528\u8fd9\u4e9b\u5f02\u5e38\u4e86\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    msg = s.recv()\nexcept TimeoutError as e:\n    ...\nexcept ProtocolError as e:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\u5e94\u8be5\u603b\u662f\u7ee7\u627f\u81ea\u5185\u7f6e\u7684 Exception \u7c7b\uff0c\n\u6216\u8005\u662f\u7ee7\u627f\u81ea\u90a3\u4e9b\u672c\u8eab\u5c31\u662f\u4ece Exception \u7ee7\u627f\u800c\u6765\u7684\u7c7b\u3002\n\u5c3d\u7ba1\u6240\u6709\u7c7b\u540c\u65f6\u4e5f\u7ee7\u627f\u81ea BaseException \uff0c\u4f46\u4f60\u4e0d\u5e94\u8be5\u4f7f\u7528\u8fd9\u4e2a\u57fa\u7c7b\u6765\u5b9a\u4e49\u65b0\u7684\u5f02\u5e38\u3002\nBaseException \u662f\u4e3a\u7cfb\u7edf\u9000\u51fa\u5f02\u5e38\u800c\u4fdd\u7559\u7684\uff0c\u6bd4\u5982 KeyboardInterrupt \u6216 SystemExit\n\u4ee5\u53ca\u5176\u4ed6\u90a3\u4e9b\u4f1a\u7ed9\u5e94\u7528\u53d1\u9001\u4fe1\u53f7\u800c\u9000\u51fa\u7684\u5f02\u5e38\u3002\n\u56e0\u6b64\uff0c\u6355\u83b7\u8fd9\u4e9b\u5f02\u5e38\u672c\u8eab\u6ca1\u4ec0\u4e48\u610f\u4e49\u3002\n\u8fd9\u6837\u7684\u8bdd\uff0c\u5047\u5982\u4f60\u7ee7\u627f BaseException\n\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f60\u7684\u81ea\u5b9a\u4e49\u5f02\u5e38\u4e0d\u4f1a\u88ab\u6355\u83b7\u800c\u76f4\u63a5\u53d1\u9001\u4fe1\u53f7\u9000\u51fa\u7a0b\u5e8f\u8fd0\u884c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u7a0b\u5e8f\u4e2d\u5f15\u5165\u81ea\u5b9a\u4e49\u5f02\u5e38\u53ef\u4ee5\u4f7f\u5f97\u4f60\u7684\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u80fd\u6e05\u6670\u663e\u793a\u8c01\u5e94\u8be5\u9605\u8bfb\u8fd9\u4e2a\u4ee3\u7801\u3002\n\u8fd8\u6709\u4e00\u79cd\u8bbe\u8ba1\u662f\u5c06\u81ea\u5b9a\u4e49\u5f02\u5e38\u901a\u8fc7\u7ee7\u627f\u7ec4\u5408\u8d77\u6765\u3002\u5728\u590d\u6742\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\n\u4f7f\u7528\u57fa\u7c7b\u6765\u5206\u7ec4\u5404\u79cd\u5f02\u5e38\u7c7b\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u5b83\u53ef\u4ee5\u8ba9\u7528\u6237\u6355\u83b7\u4e00\u4e2a\u8303\u56f4\u5f88\u7a84\u7684\u7279\u5b9a\u5f02\u5e38\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6837\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    s.send(msg)\nexcept ProtocolError:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8fd8\u80fd\u6355\u83b7\u66f4\u5927\u8303\u56f4\u7684\u5f02\u5e38\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    s.send(msg)\nexcept NetworkError:\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5b9a\u4e49\u7684\u65b0\u5f02\u5e38\u91cd\u5199\u4e86 __init__() \u65b9\u6cd5\uff0c\n\u786e\u4fdd\u4f60\u4f7f\u7528\u6240\u6709\u53c2\u6570\u8c03\u7528 Exception.__init__() \uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class CustomError(Exception):\n    def __init__(self, message, status):\n        super().__init__(message, status)\n        self.message = message\n        self.status = status"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u770b\u4e0a\u53bb\u6709\u70b9\u5947\u602a\uff0c\u4e0d\u8fc7Exception\u7684\u9ed8\u8ba4\u884c\u4e3a\u662f\u63a5\u53d7\u6240\u6709\u4f20\u9012\u7684\u53c2\u6570\u5e76\u5c06\u5b83\u4eec\u4ee5\u5143\u7ec4\u5f62\u5f0f\u5b58\u50a8\u5728 .args \u5c5e\u6027\u4e2d.\n\u5f88\u591a\u5176\u4ed6\u51fd\u6570\u5e93\u548c\u90e8\u5206Python\u5e93\u9ed8\u8ba4\u6240\u6709\u5f02\u5e38\u90fd\u5fc5\u987b\u6709 .args \u5c5e\u6027\uff0c\n\u56e0\u6b64\u5982\u679c\u4f60\u5ffd\u7565\u4e86\u8fd9\u4e00\u6b65\uff0c\u4f60\u4f1a\u53d1\u73b0\u6709\u4e9b\u65f6\u5019\u4f60\u5b9a\u4e49\u7684\u65b0\u5f02\u5e38\u4e0d\u4f1a\u6309\u7167\u671f\u671b\u8fd0\u884c\u3002\n\u4e3a\u4e86\u6f14\u793a .args \u7684\u4f7f\u7528\uff0c\u8003\u8651\u4e0b\u4e0b\u9762\u8fd9\u4e2a\u4f7f\u7528\u5185\u7f6e\u7684 RuntimeError` \u5f02\u5e38\u7684\u4ea4\u4e92\u4f1a\u8bdd\uff0c\n\u6ce8\u610f\u770braise\u8bed\u53e5\u4e2d\u4f7f\u7528\u7684\u53c2\u6570\u4e2a\u6570\u662f\u600e\u6837\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    raise RuntimeError('It failed')\nexcept RuntimeError as e:\n    print(e.args)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    raise RuntimeError('It failed', 42, 'spam')\nexcept RuntimeError as e:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "    print(e.args)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u8003`Python\u5b98\u65b9\u6587\u6863 <https://docs.python.org/3/tutorial/errors.html>`_"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.9 \u6355\u83b7\u5f02\u5e38\u540e\u629b\u51fa\u53e6\u5916\u7684\u5f02\u5e38\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u6355\u83b7\u4e00\u4e2a\u5f02\u5e38\u540e\u629b\u51fa\u53e6\u5916\u4e00\u4e2a\u4e0d\u540c\u7684\u5f02\u5e38\uff0c\u540c\u65f6\u8fd8\u5f97\u5728\u5f02\u5e38\u56de\u6eaf\u4e2d\u4fdd\u7559\u4e24\u4e2a\u5f02\u5e38\u7684\u4fe1\u606f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u94fe\u63a5\u5f02\u5e38\uff0c\u4f7f\u7528 raise from \u8bed\u53e5\u6765\u4ee3\u66ff\u7b80\u5355\u7684 raise \u8bed\u53e5\u3002\n\u5b83\u4f1a\u8ba9\u4f60\u540c\u65f6\u4fdd\u7559\u4e24\u4e2a\u5f02\u5e38\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def example():\n    try:\n            int('N/A')\n    except ValueError as e:\n            raise RuntimeError('A parsing error occurred') from e\nexample()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u7684\u5f02\u5e38\u662f\u4e0b\u9762\u7684\u5f02\u5e38\u4ea7\u751f\u7684\u76f4\u63a5\u539f\u56e0\uff1a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u56de\u6eaf\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u4e24\u4e2a\u5f02\u5e38\u90fd\u88ab\u6355\u83b7\u3002\n\u8981\u60f3\u6355\u83b7\u8fd9\u6837\u7684\u5f02\u5e38\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684 except \u8bed\u53e5\u3002\n\u4e0d\u8fc7\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u5f02\u5e38\u5bf9\u8c61\u7684 __cause__ \u5c5e\u6027\u6765\u8ddf\u8e2a\u5f02\u5e38\u94fe\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    example()\nexcept RuntimeError as e:\n    print(\"It didn't work:\", e)\n\n    if e.__cause__:\n        print('Cause:', e.__cause__)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u5728 except \u5757\u4e2d\u53c8\u6709\u53e6\u5916\u7684\u5f02\u5e38\u88ab\u629b\u51fa\u65f6\u4f1a\u5bfc\u81f4\u4e00\u4e2a\u9690\u85cf\u7684\u5f02\u5e38\u94fe\u7684\u51fa\u73b0\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def example2():\n    try:\n            int('N/A')\n    except ValueError as e:\n            print(\"Couldn't parse:\", err)\nexample2()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5904\u7406\u4e0a\u8ff0\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u53e6\u5916\u4e00\u4e2a\u5f02\u5e38\u53d1\u751f\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f60\u540c\u65f6\u83b7\u5f97\u4e86\u4e24\u4e2a\u5f02\u5e38\u7684\u4fe1\u606f\uff0c\u4f46\u662f\u5bf9\u5f02\u5e38\u7684\u89e3\u91ca\u4e0d\u540c\u3002\n\u8fd9\u65f6\u5019\uff0cNameError \u5f02\u5e38\u88ab\u4f5c\u4e3a\u7a0b\u5e8f\u6700\u7ec8\u5f02\u5e38\u88ab\u629b\u51fa\uff0c\u800c\u4e0d\u662f\u4f4d\u4e8e\u89e3\u6790\u5f02\u5e38\u7684\u76f4\u63a5\u56de\u5e94\u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\uff0c\u4f60\u60f3\u5ffd\u7565\u6389\u5f02\u5e38\u94fe\uff0c\u53ef\u4f7f\u7528 raise from None :"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def example3():\n    try:\n            int('N/A')\n    except ValueError:\n            raise RuntimeError('A parsing error occurred') from None"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8bbe\u8ba1\u4ee3\u7801\u65f6\uff0c\u5728\u53e6\u5916\u4e00\u4e2a except \u4ee3\u7801\u5757\u4e2d\u4f7f\u7528 raise \u8bed\u53e5\u7684\u65f6\u5019\u4f60\u8981\u7279\u522b\u5c0f\u5fc3\u4e86\u3002\n\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd raise \u8bed\u53e5\u90fd\u5e94\u8be5\u88ab\u6539\u6210 raise from \u8bed\u53e5\u3002\u4e5f\u5c31\u662f\u8bf4\u4f60\u5e94\u8be5\u4f7f\u7528\u4e0b\u9762\u8fd9\u79cd\u5f62\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n   ...\nexcept SomeException as e:\n   raise DifferentException() from e"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u505a\u7684\u539f\u56e0\u662f\u4f60\u5e94\u8be5\u663e\u793a\u7684\u5c06\u539f\u56e0\u94fe\u63a5\u8d77\u6765\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0cDifferentException \u662f\u76f4\u63a5\u4ece SomeException \u884d\u751f\u800c\u6765\u3002\n\u8fd9\u79cd\u5173\u7cfb\u53ef\u4ee5\u4ece\u56de\u6eaf\u7ed3\u679c\u4e2d\u770b\u51fa\u6765\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\u4ee3\u7801\uff0c\u4f60\u4ecd\u7136\u4f1a\u5f97\u5230\u4e00\u4e2a\u94fe\u63a5\u5f02\u5e38\uff0c\n\u4e0d\u8fc7\u8fd9\u4e2a\u5e76\u6ca1\u6709\u5f88\u6e05\u6670\u7684\u8bf4\u660e\u8fd9\u4e2a\u5f02\u5e38\u94fe\u5230\u5e95\u662f\u5185\u90e8\u5f02\u5e38\u8fd8\u662f\u67d0\u4e2a\u672a\u77e5\u7684\u7f16\u7a0b\u9519\u8bef\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n   ...\nexcept SomeException:\n   raise DifferentException()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u4f7f\u7528 raise from \u8bed\u53e5\u7684\u8bdd\uff0c\u5c31\u5f88\u6e05\u695a\u7684\u8868\u660e\u629b\u51fa\u7684\u662f\u7b2c\u4e8c\u4e2a\u5f02\u5e38\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u9690\u85cf\u5f02\u5e38\u94fe\u4fe1\u606f\u3002\n\u5c3d\u7ba1\u9690\u85cf\u5f02\u5e38\u94fe\u4fe1\u606f\u4e0d\u5229\u4e8e\u56de\u6eaf\uff0c\u540c\u65f6\u5b83\u4e5f\u4e22\u5931\u4e86\u5f88\u591a\u6709\u7528\u7684\u8c03\u8bd5\u4fe1\u606f\u3002\n\u4e0d\u8fc7\u4e07\u4e8b\u7686\u5e73\u7b49\uff0c\u6709\u65f6\u5019\u53ea\u4fdd\u7559\u9002\u5f53\u7684\u4fe1\u606f\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.10 \u91cd\u65b0\u629b\u51fa\u88ab\u6355\u83b7\u7684\u5f02\u5e38\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5728\u4e00\u4e2a except \u5757\u4e2d\u6355\u83b7\u4e86\u4e00\u4e2a\u5f02\u5e38\uff0c\u73b0\u5728\u60f3\u91cd\u65b0\u629b\u51fa\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7b80\u5355\u7684\u4f7f\u7528\u4e00\u4e2a\u5355\u72ec\u7684 rasie \u8bed\u53e5\u5373\u53ef\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def example():\n    try:\n            int('N/A')\n    except ValueError:\n            print(\"Didn't work\")\n            raise"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "example()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u95ee\u9898\u901a\u5e38\u662f\u5f53\u4f60\u9700\u8981\u5728\u6355\u83b7\u5f02\u5e38\u540e\u6267\u884c\u67d0\u4e2a\u64cd\u4f5c\uff08\u6bd4\u5982\u8bb0\u5f55\u65e5\u5fd7\u3001\u6e05\u7406\u7b49\uff09\uff0c\u4f46\u662f\u4e4b\u540e\u60f3\u5c06\u5f02\u5e38\u4f20\u64ad\u4e0b\u53bb\u3002\n\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u7528\u6cd5\u662f\u5728\u6355\u83b7\u6240\u6709\u5f02\u5e38\u7684\u5904\u7406\u5668\u4e2d\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n   ...\nexcept Exception as e:\n   # Process exception information in some way\n   ...\n\n   # Propagate the exception\n   raise"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.11 \u8f93\u51fa\u8b66\u544a\u4fe1\u606f\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u5e0c\u671b\u81ea\u5df1\u7684\u7a0b\u5e8f\u80fd\u751f\u6210\u8b66\u544a\u4fe1\u606f\uff08\u6bd4\u5982\u5e9f\u5f03\u7279\u6027\u6216\u4f7f\u7528\u95ee\u9898\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u8f93\u51fa\u4e00\u4e2a\u8b66\u544a\u6d88\u606f\uff0c\u53ef\u4f7f\u7528 warning.warn() \u51fd\u6570\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import warnings\n\ndef func(x, y, logfile=None, debug=False):\n    if logfile is not None:\n         warnings.warn('logfile argument deprecated', DeprecationWarning)\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "warn() \u7684\u53c2\u6570\u662f\u4e00\u4e2a\u8b66\u544a\u6d88\u606f\u548c\u4e00\u4e2a\u8b66\u544a\u7c7b\uff0c\u8b66\u544a\u7c7b\u6709\u5982\u4e0b\u51e0\u79cd\uff1aUserWarning,  DeprecationWarning,\nSyntaxWarning, RuntimeWarning, ResourceWarning, \u6216 FutureWarning."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u8b66\u544a\u7684\u5904\u7406\u53d6\u51b3\u4e8e\u4f60\u5982\u4f55\u8fd0\u884c\u89e3\u91ca\u5668\u4ee5\u53ca\u4e00\u4e9b\u5176\u4ed6\u914d\u7f6e\u3002\n\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528 -W all \u9009\u9879\u53bb\u8fd0\u884cPython\uff0c\u4f60\u4f1a\u5f97\u5230\u5982\u4e0b\u7684\u8f93\u51fa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 -W all example.py\nexample.py:5: DeprecationWarning: logfile argument is deprecated\n  warnings.warn('logfile argument is deprecated', DeprecationWarning)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u8b66\u544a\u4f1a\u8f93\u51fa\u5230\u6807\u51c6\u9519\u8bef\u4e0a\u3002\u5982\u679c\u4f60\u60f3\u8bb2\u8b66\u544a\u8f6c\u6362\u4e3a\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528 -W error \u9009\u9879\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 -W error example.py\nTraceback (most recent call last):\n  File \"example.py\", line 10, in <module>\n    func(2, 3, logfile='log.txt')\n  File \"example.py\", line 5, in func\n    warnings.warn('logfile argument is deprecated', DeprecationWarning)\nDeprecationWarning: logfile argument is deprecated\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f60\u7ef4\u62a4\u8f6f\u4ef6\uff0c\u63d0\u793a\u7528\u6237\u67d0\u4e9b\u4fe1\u606f\uff0c\u4f46\u662f\u53c8\u4e0d\u9700\u8981\u5c06\u5176\u4e0a\u5347\u4e3a\u5f02\u5e38\u7ea7\u522b\uff0c\u90a3\u4e48\u8f93\u51fa\u8b66\u544a\u4fe1\u606f\u5c31\u4f1a\u5f88\u6709\u7528\u4e86\u3002\n\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u51c6\u5907\u4fee\u6539\u67d0\u4e2a\u51fd\u6570\u5e93\u6216\u6846\u67b6\u7684\u529f\u80fd\uff0c\u4f60\u53ef\u4ee5\u5148\u4e3a\u4f60\u8981\u66f4\u6539\u7684\u90e8\u5206\u8f93\u51fa\u8b66\u544a\u4fe1\u606f\uff0c\u540c\u65f6\u5411\u540e\u517c\u5bb9\u4e00\u6bb5\u65f6\u95f4\u3002\n\u4f60\u8fd8\u53ef\u4ee5\u8b66\u544a\u7528\u6237\u4e00\u4e9b\u5bf9\u4ee3\u7801\u6709\u95ee\u9898\u7684\u4f7f\u7528\u65b9\u5f0f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f5c\u4e3a\u53e6\u5916\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\u5e93\u7684\u8b66\u544a\u4f7f\u7528\u4f8b\u5b50\uff0c\u4e0b\u9762\u6f14\u793a\u4e86\u4e00\u4e2a\u6ca1\u6709\u5173\u95ed\u6587\u4ef6\u5c31\u9500\u6bc1\u5b83\u65f6\u4ea7\u751f\u7684\u8b66\u544a\u6d88\u606f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import warnings\nwarnings.simplefilter('always')\nf = open('/etc/passwd')\ndel f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u8b66\u544a\u6d88\u606f\u90fd\u4f1a\u51fa\u73b0\u3002-W \u9009\u9879\u80fd\u63a7\u5236\u8b66\u544a\u6d88\u606f\u7684\u8f93\u51fa\u3002\n-W all \u4f1a\u8f93\u51fa\u6240\u6709\u8b66\u544a\u6d88\u606f\uff0c-W ignore \u5ffd\u7565\u6389\u6240\u6709\u8b66\u544a\uff0c-W error \u5c06\u8b66\u544a\u8f6c\u6362\u6210\u5f02\u5e38\u3002\n\u53e6\u5916\u4e00\u79cd\u9009\u62e9\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 warnings.simplefilter() \u51fd\u6570\u63a7\u5236\u8f93\u51fa\u3002\nalways \u53c2\u6570\u4f1a\u8ba9\u6240\u6709\u8b66\u544a\u6d88\u606f\u51fa\u73b0\uff0c`ignore \u5ffd\u7565\u8c03\u6240\u6709\u7684\u8b66\u544a\uff0cerror \u5c06\u8b66\u544a\u8f6c\u6362\u6210\u5f02\u5e38\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u7b80\u5355\u7684\u751f\u6210\u8b66\u544a\u6d88\u606f\u7684\u60c5\u51b5\u8fd9\u4e9b\u5df2\u7ecf\u8db3\u591f\u4e86\u3002\nwarnings \u6a21\u5757\u5bf9\u8fc7\u6ee4\u548c\u8b66\u544a\u6d88\u606f\u5904\u7406\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u66f4\u9ad8\u7ea7\u7684\u914d\u7f6e\u9009\u9879\u3002\n\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u8003 Python\u6587\u6863"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.12 \u8c03\u8bd5\u57fa\u672c\u7684\u7a0b\u5e8f\u5d29\u6e83\u9519\u8bef\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u7a0b\u5e8f\u5d29\u6e83\u540e\u8be5\u600e\u6837\u53bb\u8c03\u8bd5\u5b83\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u56e0\u4e3a\u67d0\u4e2a\u5f02\u5e38\u800c\u5d29\u6e83\uff0c\u8fd0\u884c python3 -i someprogram.py \u53ef\u6267\u884c\u7b80\u5355\u7684\u8c03\u8bd5\u3002\n-i \u9009\u9879\u53ef\u8ba9\u7a0b\u5e8f\u7ed3\u675f\u540e\u6253\u5f00\u4e00\u4e2a\u4ea4\u4e92\u5f0fshell\u3002\n\u7136\u540e\u4f60\u5c31\u80fd\u67e5\u770b\u73af\u5883\uff0c\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e0b\u9762\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# sample.py\n\ndef func(n):\n    return n + 10\n\nfunc('Hello')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd0\u884c python3 -i sample.py \u4f1a\u6709\u7c7b\u4f3c\u5982\u4e0b\u7684\u8f93\u51fa\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "func(10)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u770b\u4e0d\u5230\u4e0a\u9762\u8fd9\u6837\u7684\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u5d29\u6e83\u540e\u6253\u5f00Python\u7684\u8c03\u8bd5\u5668\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pdb\npdb.pm()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7684\u4ee3\u7801\u6240\u5728\u7684\u73af\u5883\u5f88\u96be\u83b7\u53d6\u4ea4\u4e92shell\uff08\u6bd4\u5982\u5728\u67d0\u4e2a\u670d\u52a1\u5668\u4e0a\u9762\uff09\uff0c\n\u901a\u5e38\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\u540e\u81ea\u5df1\u6253\u5370\u8ddf\u8e2a\u4fe1\u606f\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import traceback\nimport sys\n\ntry:\n    func(arg)\nexcept:\n    print('**** AN ERROR OCCURRED ****')\n    traceback.print_exc(file=sys.stderr)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u662f\u4f60\u7684\u7a0b\u5e8f\u6ca1\u6709\u5d29\u6e83\uff0c\u800c\u53ea\u662f\u4ea7\u751f\u4e86\u4e00\u4e9b\u4f60\u770b\u4e0d\u61c2\u7684\u7ed3\u679c\uff0c\n\u4f60\u5728\u611f\u5174\u8da3\u7684\u5730\u65b9\u63d2\u5165\u4e00\u4e0b print() \u8bed\u53e5\u4e5f\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\n\u4e0d\u8fc7\uff0c\u8981\u662f\u4f60\u6253\u7b97\u8fd9\u6837\u505a\uff0c\u6709\u4e00\u4e9b\u5c0f\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u3002\n\u9996\u5148\uff0ctraceback.print_stack() \u51fd\u6570\u4f1a\u4f60\u7a0b\u5e8f\u8fd0\u884c\u5230\u90a3\u4e2a\u70b9\u7684\u65f6\u5019\u521b\u5efa\u4e00\u4e2a\u8ddf\u8e2a\u6808\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def sample(n):\n    if n > 0:\n            sample(n-1)\n    else:\n            traceback.print_stack(file=sys.stderr)\nsample(5)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u4f7f\u7528 pdb.set_trace() \u5728\u4efb\u4f55\u5730\u65b9\u624b\u52a8\u7684\u542f\u52a8\u8c03\u8bd5\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pdb\n\ndef func(arg):\n    ...\n    pdb.set_trace()\n    ..."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u7a0b\u5e8f\u6bd4\u8f83\u5927\u800c\u4f60\u60f3\u8c03\u8bd5\u63a7\u5236\u6d41\u7a0b\u4ee5\u53ca\u51fd\u6570\u53c2\u6570\u7684\u65f6\u5019\u8fd9\u4e2a\u5c31\u6bd4\u8f83\u6709\u7528\u4e86\u3002\n\u4f8b\u5982\uff0c\u4e00\u65e6\u8c03\u8bd5\u5668\u5f00\u59cb\u8fd0\u884c\uff0c\u4f60\u5c31\u80fd\u591f\u4f7f\u7528 print \u6765\u89c2\u6d4b\u53d8\u91cf\u503c\u6216\u6572\u51fb\u67d0\u4e2a\u547d\u4ee4\u6bd4\u5982 w \u6765\u83b7\u53d6\u8ffd\u8e2a\u4fe1\u606f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8981\u5c06\u8c03\u8bd5\u5f04\u7684\u8fc7\u4e8e\u590d\u6742\u5316\u3002\u4e00\u4e9b\u7b80\u5355\u7684\u9519\u8bef\u53ea\u9700\u8981\u89c2\u5bdf\u7a0b\u5e8f\u5806\u6808\u4fe1\u606f\u5c31\u80fd\u77e5\u9053\u4e86\uff0c\n\u5b9e\u9645\u7684\u9519\u8bef\u4e00\u822c\u662f\u5806\u6808\u7684\u6700\u540e\u4e00\u884c\u3002\n\u4f60\u5728\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u4e5f\u53ef\u4ee5\u5728\u4f60\u9700\u8981\u8c03\u8bd5\u7684\u5730\u65b9\u63d2\u5165\u4e00\u4e0b print()\n\u51fd\u6570\u6765\u8bca\u65ad\u4fe1\u606f\uff08\u53ea\u9700\u8981\u6700\u540e\u53d1\u5e03\u7684\u65f6\u5019\u5220\u9664\u8fd9\u4e9b\u6253\u5370\u8bed\u53e5\u5373\u53ef\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8c03\u8bd5\u5668\u7684\u4e00\u4e2a\u5e38\u89c1\u7528\u6cd5\u662f\u89c2\u6d4b\u67d0\u4e2a\u5df2\u7ecf\u5d29\u6e83\u7684\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u3002\n\u77e5\u9053\u600e\u6837\u5728\u51fd\u6570\u5d29\u6e83\u540e\u8fdb\u5165\u8c03\u8bd5\u5668\u662f\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u6280\u80fd\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u60f3\u89e3\u5256\u4e00\u4e2a\u975e\u5e38\u590d\u6742\u7684\u7a0b\u5e8f\uff0c\u5e95\u5c42\u7684\u63a7\u5236\u903b\u8f91\u4f60\u4e0d\u662f\u5f88\u6e05\u695a\u7684\u65f6\u5019\uff0c\n\u63d2\u5165 pdb.set_trace() \u8fd9\u6837\u7684\u8bed\u53e5\u5c31\u5f88\u6709\u7528\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9e\u9645\u4e0a\uff0c\u7a0b\u5e8f\u4f1a\u4e00\u76f4\u8fd0\u884c\u5230\u78b0\u5230 set_trace() \u8bed\u53e5\u4f4d\u7f6e\uff0c\u7136\u540e\u7acb\u9a6c\u8fdb\u5165\u8c03\u8bd5\u5668\u3002\n\u7136\u540e\u4f60\u5c31\u53ef\u4ee5\u505a\u66f4\u591a\u7684\u4e8b\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u4f7f\u7528IDE\u6765\u505aPython\u5f00\u53d1\uff0c\u901a\u5e38IDE\u90fd\u4f1a\u63d0\u4f9b\u81ea\u5df1\u7684\u8c03\u8bd5\u5668\u6765\u66ff\u4ee3pdb\u3002\n\u66f4\u591a\u8fd9\u65b9\u9762\u7684\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003\u4f60\u4f7f\u7528\u7684IDE\u624b\u518c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.13 \u7ed9\u4f60\u7684\u7a0b\u5e8f\u505a\u6027\u80fd\u6d4b\u8bd5\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u6d4b\u8bd5\u4f60\u7684\u7a0b\u5e8f\u8fd0\u884c\u6240\u82b1\u8d39\u7684\u65f6\u95f4\u5e76\u505a\u6027\u80fd\u6d4b\u8bd5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u53ea\u662f\u7b80\u5355\u7684\u60f3\u6d4b\u8bd5\u4e0b\u4f60\u7684\u7a0b\u5e8f\u6574\u4f53\u82b1\u8d39\u7684\u65f6\u95f4\uff0c\n\u901a\u5e38\u4f7f\u7528Unix\u65f6\u95f4\u51fd\u6570\u5c31\u884c\u4e86\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % time python3 someprogram.py\nreal 0m13.937s\nuser 0m12.162s\nsys  0m0.098s\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u8fd8\u9700\u8981\u4e00\u4e2a\u7a0b\u5e8f\u5404\u4e2a\u7ec6\u8282\u7684\u8be6\u7ec6\u62a5\u544a\uff0c\u53ef\u4ee5\u4f7f\u7528 cProfile \u6a21\u5757\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "bash % python3 -m cProfile someprogram.py\n         859647 function calls in 16.016 CPU seconds\n\n   Ordered by: standard name\n\n   ncalls  tottime  percall  cumtime  percall filename:lineno(function)\n   263169    0.080    0.000    0.080    0.000 someprogram.py:16(frange)\n      513    0.001    0.000    0.002    0.000 someprogram.py:30(generate_mandel)\n   262656    0.194    0.000   15.295    0.000 someprogram.py:32(<genexpr>)\n        1    0.036    0.036   16.077   16.077 someprogram.py:4(<module>)\n   262144   15.021    0.000   15.021    0.000 someprogram.py:4(in_mandelbrot)\n        1    0.000    0.000    0.000    0.000 os.py:746(urandom)\n        1    0.000    0.000    0.000    0.000 png.py:1056(_readable)\n        1    0.000    0.000    0.000    0.000 png.py:1073(Reader)\n        1    0.227    0.227    0.438    0.438 png.py:163(<module>)\n      512    0.010    0.000    0.010    0.000 png.py:200(group)\n    ...\nbash %"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8fc7\u901a\u5e38\u60c5\u51b5\u662f\u4ecb\u4e8e\u8fd9\u4e24\u4e2a\u6781\u7aef\u4e4b\u95f4\u3002\u6bd4\u5982\u4f60\u5df2\u7ecf\u77e5\u9053\u4ee3\u7801\u8fd0\u884c\u65f6\u5728\u5c11\u6570\u51e0\u4e2a\u51fd\u6570\u4e2d\u82b1\u8d39\u4e86\u7edd\u5927\u90e8\u5206\u65f6\u95f4\u3002\n\u5bf9\u4e8e\u8fd9\u4e9b\u51fd\u6570\u7684\u6027\u80fd\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u88c5\u9970\u5668\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# timethis.py\n\nimport time\nfrom functools import wraps\n\ndef timethis(func):\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        start = time.perf_counter()\n        r = func(*args, **kwargs)\n        end = time.perf_counter()\n        print('{}.{} : {}'.format(func.__module__, func.__name__, end - start))\n        return r\n    return wrapper"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u4f7f\u7528\u8fd9\u4e2a\u88c5\u9970\u5668\uff0c\u53ea\u9700\u8981\u5c06\u5176\u653e\u7f6e\u5728\u4f60\u8981\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u7684\u51fd\u6570\u5b9a\u4e49\u524d\u5373\u53ef\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@timethis\ndef countdown(n):\n    while n > 0:\n            n -= 1\ncountdown(10000000)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8981\u6d4b\u8bd5\u67d0\u4e2a\u4ee3\u7801\u5757\u8fd0\u884c\u65f6\u95f4\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from contextlib import contextmanager\n\n@contextmanager\ndef timeblock(label):\n    start = time.perf_counter()\n    try:\n        yield\n    finally:\n        end = time.perf_counter()\n        print('{} : {}'.format(label, end - start))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u662f\u4f7f\u7528\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u4f8b\u5b50\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with timeblock('counting'):\n    n = 10000000\n    while n > 0:\n            n -= 1"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u6d4b\u8bd5\u5f88\u5c0f\u7684\u4ee3\u7801\u7247\u6bb5\u8fd0\u884c\u6027\u80fd\uff0c\u4f7f\u7528 timeit \u6a21\u5757\u4f1a\u5f88\u65b9\u4fbf\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from timeit import timeit\ntimeit('math.sqrt(2)', 'import math')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "timeit('sqrt(2)', 'from math import sqrt')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "timeit \u4f1a\u6267\u884c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e2d\u8bed\u53e5100\u4e07\u6b21\u5e76\u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4\u3002\n\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8fd0\u884c\u6d4b\u8bd5\u4e4b\u524d\u914d\u7f6e\u73af\u5883\u3002\u5982\u679c\u4f60\u60f3\u6539\u53d8\u5faa\u73af\u6267\u884c\u6b21\u6570\uff0c\n\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u8bbe\u7f6e number \u53c2\u6570\u7684\u503c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "timeit('math.sqrt(2)', 'import math', number=10000000)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "timeit('sqrt(2)', 'from math import sqrt', number=10000000)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u6267\u884c\u6027\u80fd\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u4f60\u83b7\u53d6\u7684\u7ed3\u679c\u90fd\u662f\u8fd1\u4f3c\u503c\u3002\ntime.perf_counter() \u51fd\u6570\u4f1a\u5728\u7ed9\u5b9a\u5e73\u53f0\u4e0a\u83b7\u53d6\u6700\u9ad8\u7cbe\u5ea6\u7684\u8ba1\u65f6\u503c\u3002\n\u4e0d\u8fc7\uff0c\u5b83\u4ecd\u7136\u8fd8\u662f\u57fa\u4e8e\u65f6\u949f\u65f6\u95f4\uff0c\u5f88\u591a\u56e0\u7d20\u4f1a\u5f71\u54cd\u5230\u5b83\u7684\u7cbe\u786e\u5ea6\uff0c\u6bd4\u5982\u673a\u5668\u8d1f\u8f7d\u3002\n\u5982\u679c\u4f60\u5bf9\u4e8e\u6267\u884c\u65f6\u95f4\u66f4\u611f\u5174\u8da3\uff0c\u4f7f\u7528 time.process_time() \u6765\u4ee3\u66ff\u5b83\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import wraps\ndef timethis(func):\n    @wraps(func)\n    def wrapper(*args, **kwargs):\n        start = time.process_time()\n        r = func(*args, **kwargs)\n        end = time.process_time()\n        print('{}.{} : {}'.format(func.__module__, func.__name__, end - start))\n        return r\n    return wrapper"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u5982\u679c\u4f60\u60f3\u8fdb\u884c\u66f4\u6df1\u5165\u7684\u6027\u80fd\u5206\u6790\uff0c\u90a3\u4e48\u4f60\u9700\u8981\u8be6\u7ec6\u9605\u8bfb time \u3001timeit \u548c\u5176\u4ed6\u76f8\u5173\u6a21\u5757\u7684\u6587\u6863\u3002\n\u8fd9\u6837\u4f60\u53ef\u4ee5\u7406\u89e3\u548c\u5e73\u53f0\u76f8\u5173\u7684\u5dee\u5f02\u4ee5\u53ca\u4e00\u4e9b\u5176\u4ed6\u9677\u9631\u3002\n\u8fd8\u53ef\u4ee5\u53c2\u800313.13\u5c0f\u8282\u4e2d\u76f8\u5173\u7684\u4e00\u4e2a\u521b\u5efa\u8ba1\u65f6\u5668\u7c7b\u7684\u4f8b\u5b50\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.14 \u52a0\u901f\u7a0b\u5e8f\u8fd0\u884c\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u7684\u7a0b\u5e8f\u8fd0\u884c\u592a\u6162\uff0c\u4f60\u60f3\u5728\u4e0d\u4f7f\u7528\u590d\u6742\u6280\u672f\u6bd4\u5982C\u6269\u5c55\u6216JIT\u7f16\u8bd1\u5668\u7684\u60c5\u51b5\u4e0b\u52a0\u5feb\u7a0b\u5e8f\u8fd0\u884c\u901f\u5ea6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e\u7a0b\u5e8f\u4f18\u5316\u7684\u7b2c\u4e00\u4e2a\u51c6\u5219\u662f\u201c\u4e0d\u8981\u4f18\u5316\u201d\uff0c\u7b2c\u4e8c\u4e2a\u51c6\u5219\u662f\u201c\u4e0d\u8981\u4f18\u5316\u90a3\u4e9b\u65e0\u5173\u7d27\u8981\u7684\u90e8\u5206\u201d\u3002\n\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u8fd0\u884c\u7f13\u6162\uff0c\u9996\u5148\u4f60\u5f97\u4f7f\u752814.13\u5c0f\u8282\u7684\u6280\u672f\u5148\u5bf9\u5b83\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u627e\u5230\u95ee\u9898\u6240\u5728\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\u4f60\u4f1a\u53d1\u73b0\u4f60\u5f97\u7a0b\u5e8f\u5728\u5c11\u6570\u51e0\u4e2a\u70ed\u70b9\u5730\u65b9\u82b1\u8d39\u4e86\u5927\u91cf\u65f6\u95f4\uff0c\n\u6bd4\u5982\u5185\u5b58\u7684\u6570\u636e\u5904\u7406\u5faa\u73af\u3002\u4e00\u65e6\u4f60\u5b9a\u4f4d\u5230\u8fd9\u4e9b\u70b9\uff0c\u4f60\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u8fd9\u4e9b\u5b9e\u7528\u6280\u672f\u6765\u52a0\u901f\u7a0b\u5e8f\u8fd0\u884c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u51fd\u6570"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u591a\u7a0b\u5e8f\u5458\u521a\u5f00\u59cb\u4f1a\u4f7f\u7528Python\u8bed\u8a00\u5199\u4e00\u4e9b\u7b80\u5355\u811a\u672c\u3002\n\u5f53\u7f16\u5199\u811a\u672c\u7684\u65f6\u5019\uff0c\u901a\u5e38\u4e60\u60ef\u4e86\u5199\u6beb\u65e0\u7ed3\u6784\u7684\u4ee3\u7801\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# somescript.py\n\nimport sys\nimport csv\n\nwith open(sys.argv[1]) as f:\n     for row in csv.reader(f):\n\n         # Some kind of processing\n         pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f88\u5c11\u6709\u4eba\u77e5\u9053\uff0c\u50cf\u8fd9\u6837\u5b9a\u4e49\u5728\u5168\u5c40\u8303\u56f4\u7684\u4ee3\u7801\u8fd0\u884c\u8d77\u6765\u8981\u6bd4\u5b9a\u4e49\u5728\u51fd\u6570\u4e2d\u8fd0\u884c\u6162\u7684\u591a\u3002\n\u8fd9\u79cd\u901f\u5ea6\u5dee\u5f02\u662f\u7531\u4e8e\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u7684\u5b9e\u73b0\u65b9\u5f0f\uff08\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u8981\u66f4\u5feb\u4e9b\uff09\u3002\n\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u8ba9\u7a0b\u5e8f\u8fd0\u884c\u66f4\u5feb\u4e9b\uff0c\u53ea\u9700\u8981\u5c06\u811a\u672c\u8bed\u53e5\u653e\u5165\u51fd\u6570\u4e2d\u5373\u53ef\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# somescript.py\nimport sys\nimport csv\n\ndef main(filename):\n    with open(filename) as f:\n         for row in csv.reader(f):\n             # Some kind of processing\n             pass\n\nmain(sys.argv[1])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901f\u5ea6\u7684\u5dee\u5f02\u53d6\u51b3\u4e8e\u5b9e\u9645\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u4e0d\u8fc7\u6839\u636e\u7ecf\u9a8c\uff0c\u4f7f\u7528\u51fd\u6570\u5e26\u676515-30%\u7684\u6027\u80fd\u63d0\u5347\u662f\u5f88\u5e38\u89c1\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u53ef\u80fd\u53bb\u6389\u5c5e\u6027\u8bbf\u95ee"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6bcf\u4e00\u6b21\u4f7f\u7528\u70b9(.)\u64cd\u4f5c\u7b26\u6765\u8bbf\u95ee\u5c5e\u6027\u7684\u65f6\u5019\u4f1a\u5e26\u6765\u989d\u5916\u7684\u5f00\u9500\u3002\n\u5b83\u4f1a\u89e6\u53d1\u7279\u5b9a\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982 __getattribute__() \u548c __getattr__() \uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4f1a\u8fdb\u884c\u5b57\u5178\u64cd\u4f5c\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u4f60\u53ef\u4ee5\u4f7f\u7528 from module import name \u8fd9\u6837\u7684\u5bfc\u5165\u5f62\u5f0f\uff0c\u4ee5\u53ca\u4f7f\u7528\u7ed1\u5b9a\u7684\u65b9\u6cd5\u3002\n\u5047\u8bbe\u4f60\u6709\u5982\u4e0b\u7684\u4ee3\u7801\u7247\u6bb5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import math\n\ndef compute_roots(nums):\n    result = []\n    for n in nums:\n        result.append(math.sqrt(n))\n    return result\n\n# Test\nnums = range(1000000)\nfor n in range(100):\n    r = compute_roots(nums)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u6211\u4eec\u673a\u5668\u4e0a\u9762\u6d4b\u8bd5\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u82b1\u8d39\u4e86\u5927\u698240\u79d2\u3002\u73b0\u5728\u6211\u4eec\u4fee\u6539 compute_roots() \u51fd\u6570\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from math import sqrt\n\ndef compute_roots(nums):\n\n    result = []\n    result_append = result.append\n    for n in nums:\n        result_append(sqrt(n))\n    return result"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4fee\u6539\u540e\u7684\u7248\u672c\u8fd0\u884c\u65f6\u95f4\u5927\u6982\u662f29\u79d2\u3002\u552f\u4e00\u4e0d\u540c\u4e4b\u5904\u5c31\u662f\u6d88\u9664\u4e86\u5c5e\u6027\u8bbf\u95ee\u3002\n\u7528 sqrt() \u4ee3\u66ff\u4e86 math.sqrt() \u3002\nThe result.append() \u65b9\u6cd5\u88ab\u8d4b\u7ed9\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf result_append \uff0c\u7136\u540e\u5728\u5185\u90e8\u5faa\u73af\u4e2d\u4f7f\u7528\u5b83\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8fc7\uff0c\u8fd9\u4e9b\u6539\u53d8\u53ea\u6709\u5728\u5927\u91cf\u91cd\u590d\u4ee3\u7801\u4e2d\u624d\u6709\u610f\u4e49\uff0c\u6bd4\u5982\u5faa\u73af\u3002\n\u56e0\u6b64\uff0c\u8fd9\u4e9b\u4f18\u5316\u4e5f\u53ea\u662f\u5728\u67d0\u4e9b\u7279\u5b9a\u5730\u65b9\u624d\u5e94\u8be5\u88ab\u4f7f\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7406\u89e3\u5c40\u90e8\u53d8\u91cf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e4b\u524d\u63d0\u8fc7\uff0c\u5c40\u90e8\u53d8\u91cf\u4f1a\u6bd4\u5168\u5c40\u53d8\u91cf\u8fd0\u884c\u901f\u5ea6\u5feb\u3002\n\u5bf9\u4e8e\u9891\u7e41\u8bbf\u95ee\u7684\u540d\u79f0\uff0c\u901a\u8fc7\u5c06\u8fd9\u4e9b\u540d\u79f0\u53d8\u6210\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u52a0\u901f\u7a0b\u5e8f\u8fd0\u884c\u3002\n\u4f8b\u5982\uff0c\u770b\u4e0b\u4e4b\u524d\u5bf9\u4e8e compute_roots() \u51fd\u6570\u8fdb\u884c\u4fee\u6539\u540e\u7684\u7248\u672c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import math\n\ndef compute_roots(nums):\n    sqrt = math.sqrt\n    result = []\n    result_append = result.append\n    for n in nums:\n        result_append(sqrt(n))\n    return result"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u7248\u672c\u4e2d\uff0csqrt \u4ece match \u6a21\u5757\u88ab\u62ff\u51fa\u5e76\u653e\u5165\u4e86\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u4e2d\u3002\n\u5982\u679c\u4f60\u8fd0\u884c\u8fd9\u4e2a\u4ee3\u7801\uff0c\u5927\u6982\u82b1\u8d3925\u79d2\uff08\u5bf9\u4e8e\u4e4b\u524d29\u79d2\u53c8\u662f\u4e00\u4e2a\u6539\u8fdb\uff09\u3002\n\u8fd9\u4e2a\u989d\u5916\u7684\u52a0\u901f\u539f\u56e0\u662f\u56e0\u4e3a\u5bf9\u4e8e\u5c40\u90e8\u53d8\u91cf sqrt \u7684\u67e5\u627e\u8981\u5feb\u4e8e\u5168\u5c40\u53d8\u91cf sqrt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u7c7b\u4e2d\u7684\u5c5e\u6027\u8bbf\u95ee\u4e5f\u540c\u6837\u9002\u7528\u4e8e\u8fd9\u4e2a\u539f\u7406\u3002\n\u901a\u5e38\u6765\u8bb2\uff0c\u67e5\u627e\u67d0\u4e2a\u503c\u6bd4\u5982 self.name \u4f1a\u6bd4\u8bbf\u95ee\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u8981\u6162\u4e00\u4e9b\u3002\n\u5728\u5185\u90e8\u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u5c06\u67d0\u4e2a\u9700\u8981\u9891\u7e41\u8bbf\u95ee\u7684\u5c5e\u6027\u653e\u5165\u5230\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u4e2d\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Slower\nclass SomeClass:\n    ...\n    def method(self):\n         for x in s:\n             op(self.value)\n\n# Faster\nclass SomeClass:\n\n    ...\n    def method(self):\n         value = self.value\n         for x in s:\n             op(value)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62bd\u8c61"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4efb\u4f55\u65f6\u5019\u5f53\u4f60\u4f7f\u7528\u989d\u5916\u7684\u5904\u7406\u5c42\uff08\u6bd4\u5982\u88c5\u9970\u5668\u3001\u5c5e\u6027\u8bbf\u95ee\u3001\u63cf\u8ff0\u5668\uff09\u53bb\u5305\u88c5\u4f60\u7684\u4ee3\u7801\u65f6\uff0c\u90fd\u4f1a\u8ba9\u7a0b\u5e8f\u8fd0\u884c\u53d8\u6162\u3002\n\u6bd4\u5982\u770b\u4e0b\u5982\u4e0b\u7684\u8fd9\u4e2a\u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class A:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n    @property\n    def y(self):\n        return self._y\n    @y.setter\n    def y(self, value):\n        self._y = value"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u8fdb\u884c\u4e00\u4e2a\u7b80\u5355\u6d4b\u8bd5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from timeit import timeit\na = A(1,2)\ntimeit('a.x', 'from __main__ import a')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "timeit('a.y', 'from __main__ import a')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u770b\u5230\uff0c\u8bbf\u95ee\u5c5e\u6027y\u76f8\u6bd4\u5c5e\u6027x\u800c\u8a00\u6162\u7684\u4e0d\u6b62\u4e00\u70b9\u70b9\uff0c\u5927\u6982\u6162\u4e864.5\u500d\u3002\n\u5982\u679c\u4f60\u5728\u610f\u6027\u80fd\u7684\u8bdd\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u91cd\u65b0\u5ba1\u89c6\u4e0b\u5bf9\u4e8ey\u7684\u5c5e\u6027\u8bbf\u95ee\u5668\u7684\u5b9a\u4e49\u662f\u5426\u771f\u7684\u6709\u5fc5\u8981\u4e86\u3002\n\u5982\u679c\u6ca1\u6709\u5fc5\u8981\uff0c\u5c31\u4f7f\u7528\u7b80\u5355\u5c5e\u6027\u5427\u3002\n\u5982\u679c\u4ec5\u4ec5\u662f\u56e0\u4e3a\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u9700\u8981\u4f7f\u7528getter/setter\u51fd\u6570\u5c31\u53bb\u4fee\u6539\u4ee3\u7801\u98ce\u683c\uff0c\u8fd9\u4e2a\u771f\u7684\u6ca1\u6709\u5fc5\u8981\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u5185\u7f6e\u7684\u5bb9\u5668"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5185\u7f6e\u7684\u6570\u636e\u7c7b\u578b\u6bd4\u5982\u5b57\u7b26\u4e32\u3001\u5143\u7ec4\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u5b57\u5178\u90fd\u662f\u4f7f\u7528C\u6765\u5b9e\u73b0\u7684\uff0c\u8fd0\u884c\u8d77\u6765\u975e\u5e38\u5feb\u3002\n\u5982\u679c\u4f60\u60f3\u81ea\u5df1\u5b9e\u73b0\u65b0\u7684\u6570\u636e\u7ed3\u6784\uff08\u6bd4\u5982\u94fe\u63a5\u5217\u8868\u3001\u5e73\u8861\u6811\u7b49\uff09\uff0c\n\u90a3\u4e48\u8981\u60f3\u5728\u6027\u80fd\u4e0a\u8fbe\u5230\u5185\u7f6e\u7684\u901f\u5ea6\u51e0\u4e4e\u4e0d\u53ef\u80fd\uff0c\u56e0\u6b64\uff0c\u8fd8\u662f\u4e56\u4e56\u7684\u4f7f\u7528\u5185\u7f6e\u7684\u5427\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u907f\u514d\u521b\u5efa\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u7ed3\u6784\u6216\u590d\u5236"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u65f6\u5019\u7a0b\u5e8f\u5458\u60f3\u663e\u6446\u4e0b\uff0c\u6784\u9020\u4e00\u4e9b\u5e76\u6ca1\u6709\u5fc5\u8981\u7684\u6570\u636e\u7ed3\u6784\u3002\u4f8b\u5982\uff0c\u6709\u4eba\u53ef\u80fd\u4f1a\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "values = [x for x in sequence]\nsquares = [x*x for x in values]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e5f\u8bb8\u8fd9\u91cc\u7684\u60f3\u6cd5\u662f\u9996\u5148\u5c06\u4e00\u4e9b\u503c\u6536\u96c6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u6765\u6267\u884c\u64cd\u4f5c\u3002\n\u4e0d\u8fc7\uff0c\u7b2c\u4e00\u4e2a\u5217\u8868\u5b8c\u5168\u6ca1\u6709\u5fc5\u8981\uff0c\u53ef\u4ee5\u7b80\u5355\u7684\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "squares = [x*x for x in sequence]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0e\u6b64\u76f8\u5173\uff0c\u8fd8\u8981\u6ce8\u610f\u4e0b\u90a3\u4e9b\u5bf9Python\u7684\u5171\u4eab\u6570\u636e\u673a\u5236\u8fc7\u4e8e\u504f\u6267\u7684\u7a0b\u5e8f\u6240\u5199\u7684\u4ee3\u7801\u3002\n\u6709\u4e9b\u4eba\u5e76\u6ca1\u6709\u5f88\u597d\u7684\u7406\u89e3\u6216\u4fe1\u4efbPython\u7684\u5185\u5b58\u6a21\u578b\uff0c\u6ee5\u7528 copy.deepcopy() \u4e4b\u7c7b\u7684\u51fd\u6570\u3002\n\u901a\u5e38\u5728\u8fd9\u4e9b\u4ee3\u7801\u4e2d\u662f\u53ef\u4ee5\u53bb\u6389\u590d\u5236\u64cd\u4f5c\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4f18\u5316\u4e4b\u524d\uff0c\u6709\u5fc5\u8981\u5148\u7814\u7a76\u4e0b\u4f7f\u7528\u7684\u7b97\u6cd5\u3002\n\u9009\u62e9\u4e00\u4e2a\u590d\u6742\u5ea6\u4e3a O(n log n) \u7684\u7b97\u6cd5\u8981\u6bd4\u4f60\u53bb\u8c03\u6574\u4e00\u4e2a\u590d\u6742\u5ea6\u4e3a O(n**2) \u7684\u7b97\u6cd5\u6240\u5e26\u6765\u7684\u6027\u80fd\u63d0\u5347\u8981\u5927\u5f97\u591a\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u89c9\u5f97\u4f60\u8fd8\u662f\u5f97\u8fdb\u884c\u4f18\u5316\uff0c\u90a3\u4e48\u8bf7\u4ece\u6574\u4f53\u8003\u8651\u3002\n\u4f5c\u4e3a\u4e00\u822c\u51c6\u5219\uff0c\u4e0d\u8981\u5bf9\u7a0b\u5e8f\u7684\u6bcf\u4e00\u4e2a\u90e8\u5206\u90fd\u53bb\u4f18\u5316,\u56e0\u4e3a\u8fd9\u4e9b\u4fee\u6539\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u9605\u8bfb\u548c\u7406\u89e3\u3002\n\u4f60\u5e94\u8be5\u4e13\u6ce8\u4e8e\u4f18\u5316\u4ea7\u751f\u6027\u80fd\u74f6\u9888\u7684\u5730\u65b9\uff0c\u6bd4\u5982\u5185\u90e8\u5faa\u73af\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u8fd8\u8981\u6ce8\u610f\u5fae\u5c0f\u4f18\u5316\u7684\u7ed3\u679c\u3002\u4f8b\u5982\u8003\u8651\u4e0b\u9762\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u7684\u4e24\u79cd\u65b9\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = {\n    'name' : 'AAPL',\n    'shares' : 100,\n    'price' : 534.22\n}\n\nb = dict(name='AAPL', shares=100, price=534.22)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u540e\u9762\u4e00\u79cd\u5199\u6cd5\u66f4\u7b80\u6d01\u4e00\u4e9b\uff08\u4f60\u4e0d\u9700\u8981\u5728\u5173\u952e\u5b57\u4e0a\u8f93\u5165\u5f15\u53f7\uff09\u3002\n\u4e0d\u8fc7\uff0c\u5982\u679c\u4f60\u5c06\u8fd9\u4e24\u4e2a\u4ee3\u7801\u7247\u6bb5\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u5bf9\u6bd4\u65f6\uff0c\u4f1a\u53d1\u73b0\u4f7f\u7528 dict() \u7684\u65b9\u5f0f\u4f1a\u6162\u4e863\u500d\u3002\n\u770b\u5230\u8fd9\u4e2a\uff0c\u4f60\u662f\u4e0d\u662f\u6709\u51b2\u52a8\u628a\u6240\u6709\u4f7f\u7528 dict() \u7684\u4ee3\u7801\u90fd\u66ff\u6362\u6210\u7b2c\u4e00\u79cd\u3002\n\u4e0d\u591f\uff0c\u806a\u660e\u7684\u7a0b\u5e8f\u5458\u53ea\u4f1a\u5173\u6ce8\u4ed6\u5e94\u8be5\u5173\u6ce8\u7684\u5730\u65b9\uff0c\u6bd4\u5982\u5185\u90e8\u5faa\u73af\u3002\u5728\u5176\u4ed6\u5730\u65b9\uff0c\u8fd9\u70b9\u6027\u80fd\u635f\u5931\u6ca1\u6709\u4ec0\u4e48\u5f71\u54cd\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u7684\u4f18\u5316\u8981\u6c42\u6bd4\u8f83\u9ad8\uff0c\u672c\u8282\u7684\u8fd9\u4e9b\u7b80\u5355\u6280\u672f\u6ee1\u8db3\u4e0d\u4e86\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u7814\u7a76\u4e0b\u57fa\u4e8e\u5373\u65f6\u7f16\u8bd1\uff08JIT\uff09\u6280\u672f\u7684\u4e00\u4e9b\u5de5\u5177\u3002\n\u4f8b\u5982\uff0cPyPy\u5de5\u7a0b\u662fPython\u89e3\u91ca\u5668\u7684\u53e6\u5916\u4e00\u79cd\u5b9e\u73b0\uff0c\u5b83\u4f1a\u5206\u6790\u4f60\u7684\u7a0b\u5e8f\u8fd0\u884c\u5e76\u5bf9\u90a3\u4e9b\u9891\u7e41\u6267\u884c\u7684\u90e8\u5206\u751f\u6210\u672c\u673a\u673a\u5668\u7801\u3002\n\u5b83\u6709\u65f6\u5019\u80fd\u6781\u5927\u7684\u63d0\u5347\u6027\u80fd\uff0c\u901a\u5e38\u53ef\u4ee5\u63a5\u8fd1C\u4ee3\u7801\u7684\u901f\u5ea6\u3002\n\u4e0d\u8fc7\u53ef\u60dc\u7684\u662f\uff0c\u5230\u5199\u8fd9\u672c\u4e66\u4f4d\u7f6e\uff0cPyPy\u8fd8\u4e0d\u80fd\u5b8c\u5168\u652f\u6301Python3.\n\u56e0\u6b64\uff0c\u8fd9\u4e2a\u662f\u4f60\u5c06\u6765\u9700\u8981\u53bb\u7814\u7a76\u7684\u3002\u4f60\u8fd8\u53ef\u4ee5\u8003\u8651\u4e0bNumba\u5de5\u7a0b\uff0c\nNumba\u662f\u4e00\u4e2a\u5728\u4f60\u4f7f\u7528\u88c5\u9970\u5668\u6765\u9009\u62e9Python\u51fd\u6570\u8fdb\u884c\u4f18\u5316\u65f6\u7684\u52a8\u6001\u7f16\u8bd1\u5668\u3002\n\u8fd9\u4e9b\u51fd\u6570\u4f1a\u4f7f\u7528LLVM\u88ab\u7f16\u8bd1\u6210\u672c\u5730\u673a\u5668\u7801\u3002\u5b83\u540c\u6837\u53ef\u4ee5\u6781\u5927\u7684\u63d0\u5347\u6027\u80fd\u3002\n\u4f46\u662f\uff0c\u8ddfPyPy\u4e00\u6837\uff0c\u5b83\u5bf9\u4e8ePython 3\u7684\u652f\u6301\u73b0\u5728\u8fd8\u505c\u7559\u5728\u5b9e\u9a8c\u9636\u6bb5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u6211\u5f15\u7528John Ousterhout\u8bf4\u8fc7\u7684\u8bdd\u4f5c\u4e3a\u7ed3\u5c3e\uff1a\u201c\u6700\u597d\u7684\u6027\u80fd\u4f18\u5316\u662f\u4ece\u4e0d\u5de5\u4f5c\u5230\u5de5\u4f5c\u72b6\u6001\u7684\u8fc1\u79fb\u201d\u3002\n\u76f4\u5230\u4f60\u771f\u7684\u9700\u8981\u4f18\u5316\u7684\u65f6\u5019\u518d\u53bb\u8003\u8651\u5b83\u3002\u786e\u4fdd\u4f60\u7a0b\u5e8f\u6b63\u786e\u7684\u8fd0\u884c\u901a\u5e38\u6bd4\u8ba9\u5b83\u8fd0\u884c\u66f4\u5feb\u8981\u66f4\u91cd\u8981\u4e00\u4e9b\uff08\u81f3\u5c11\u5f00\u59cb\u662f\u8fd9\u6837\u7684\uff09."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}